<template>
	<view class="content">
		<view class="record-list" v-if="orderList.length>0">
			<view class="record_item" v-for="(item,index) in orderList" :key="index">
				<view class="order-item">
					<view class="name">{{item.companyName}} <!-- <text>(特来电都江堰倾城桥充电站)</text> --> </view>
					<view class="time">{{item.createTime}}</view>
					<view class="br"></view>
					<view class="record_info">
						<view class="left">订单ID</view>
						<view class="right">{{item.orderId}}</view>
					</view>
					<view class="record_info">
						<view class="left">支付状态</view>
						<view class="right" style="color: #31bd58;">
							{{paidStatusMap[item.paidStatus]}}
						</view>
					</view>
					<view class="record_info">
						<view class="left">充值方式</view>
						<view class="right">{{item.paidTypeName}}</view>
					</view>
					<view class="record_info">
						<view class="left">充值金额</view>
						<view class="right">{{item.amt}}元</view>
					</view>
				</view>
				<view class="refund" @click="refund(item)" v-if="index==0 && item.paidStatus==1">
					退款
				</view>
			</view>
		</view>
		<view class="hint" v-else>暂无数据!</view>
	</view>
</template>

<script>
	import {
		rechargeOrder,
		wxRefund
	} from '@/api/index'
	import {
		toast,
		showConfirm
	} from '../../../utils/common';
	export default {
		data() {
			return {
				openId: uni.getStorageSync('userOpenId'),
				orderList: [], //订单列表
				totalPage: 0,
				pageNum: 1,
				pageSize: 10,
				paidStatusMap: {
					0: '待支付',
					1: '支付成功',
					2: '退款审批中',
					3: '退款成功'
				}
			};
		},
		onLoad() {
			this.getrechargeOrder()
		},
		onPullDownRefresh() {
			this.pageNum = 1;
			this.orderList = [];
			this.getrechargeOrder();
		},
		onReachBottom() {
			if (this.pageNum < this.totalPage) {
				this.pageNum += 1;
				this.getrechargeOrder();
				return;
			}
			toast('没有更新的数据啦...')
		},
		methods: {
			getrechargeOrder() {
				rechargeOrder({
					openid: this.openId,
					pageNum: this.pageNum,
					pageSize: this.pageSize
				}).then(res => {
					uni.stopPullDownRefresh();
					if (res.rows.length > 0) {
						// this.orderList = res.rows
						this.orderList = this.orderList.concat(res.rows)
						this.totalPage = res.totalPage
						return
					}
					this.orderList = []
					this.totalPage = 0
				}).catch(error => {
					this.orderList = []
					this.totalPage = 0
					console.log(error)
				})
			},
			refund(data) {
				showConfirm(`确定对订单ID:${data.orderId}进行退款吗?`, '退款提示').then(res => {
					if (res.confirm) {
						uni.showLoading({
							title: '请求中',
						})
						wxRefund().then(res => {
							toast('申请成功')
							uni.hideLoading()
							this.orderList = []
							this.totalPage = 0
							this.getrechargeOrder();
						}).catch(error => {
							uni.hideLoading()
							toast(error)
						})
					}
				})
			}
		}
	};
</script>

<style lang="scss" scope>
	.content {
		.record-list {
			padding: 24rpx 24rpx 24rpx 24rpx;

			.record_item {
				border-radius: 16rpx;
				margin-bottom: 24rpx;
				background: #ffffff;
				box-shadow: 0px 0px 20rpx 0px rgba(135, 165, 216, 0.1);

				.order-item {
					padding: 10rpx 32rpx 10rpx 32rpx;

					.name {
						font-size: 34rpx;
						color: #2d2b2a;
						line-height: 48rpx;
						margin-bottom: 8rpx;

						text {
							font-size: 28rpx;
							color: #2d2b2a;
						}
					}

					.time {
						font-size: 26rpx;
						color: #999999;
						line-height: 72rpx;
					}

					.br {
						width: 100%;
						height: 1px;
						// border: 1px solid ;
						background-color: #ededed;
					}

					.record_info {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.left {
							font-size: 30rpx;
							color: #72798e;
							line-height: 72rpx;
						}

						.right {
							font-size: 30rpx;
							line-height: 72rpx;
							color: #333333;
						}
					}
				}

				.refund {
					text-align: center;
					line-height: 80rpx;
					background: #006241;
					color: #ffffff;
					margin-top: 20rpx;
					border-bottom-left-radius: 16rpx;
					border-bottom-right-radius: 16rpx;
				}
			}

			.record_item:nth-last-child(1) {
				margin-bottom: 0;
			}
		}

		.hint {
			width: 100%;
			height: 100vh;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 42rpx;
			color: #c0c0c0;
			letter-spacing: 10rpx;
		}
	}
</style>